<?php

namespace Core;

class AdminController extends Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->check_admin_login();
		$this->lang->load('admin_common', $this->session->admin_language);
	}

	// 加载操作成功提示页面
	public function success($redirect = '')
	{
		if (empty($redirect)) {
			$redirect = "admin/{$this->router->class}/index";
		}

		return $this->load->view('admin/success.php', [
			'redirect' => $redirect
		]);
	}

	/**
	 * 响应
	 * 生成一个响应
	 * @param mixed 数据
	 * @param int 状态码
	 * @param array 请求头
	 */
	public function response($data = null, $status = 200, array $headers = array(), $json = false)
	{
		if (is_array($data) OR $json) {
			$this->output->set_content_type('application/json');
			$this->output->set_output(json_encode($data));
		} else {
			$this->output->set_output($data);
		}

		$this->output->set_status_header($status);
		
		foreach ($headers as $key => $value) {
			if (is_integer($key)) {
				$this->output->set_header($value);
			} else {
				$this->output->set_header($key.': '.$value);
			}
		}
	}
}